package com.qianbao.service.step.impl;

import com.qianbao.components.excepion.CommonException;
import com.qianbao.dao.step.RunStepDao;
import com.qianbao.domain.step.StepEntity;
import com.qianbao.service.coinrecord.CoinRecordService;
import com.qianbao.service.money.CoinService;
import com.qianbao.service.step.RunStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RunStepServiceImpl implements RunStepService{

    @Autowired
    private RunStepDao dao;

    @Autowired
    private CoinService coinService;

    @Autowired
    private CoinRecordService coinRecordService;

    @Override
    public int stepChangeToCoin(StepEntity entity) throws CommonException {

        try{
            // 有记录
            StepEntity findEntity = dao.findRunStepWithCustomId(entity.getCustom_id());

            int preStep = findEntity.getPrechare_step();

            int currentStep = entity.getCurrent_step();

            StepEntity entity2 = new StepEntity();
            entity2.setCustom_id(entity.getCustom_id());
            entity2.setCurrent_step(entity.getCurrent_step());
            entity2.setPrechare_step(entity.getCurrent_step());

            dao.updateStepInformation(entity2);

            int num = currentStep - preStep;

            if (num > 0){

                int coin = num / 10;

                saveUserCoin(entity.getCustom_id(),coin);

                return coin;
            }else {
                return 0;
            }

        }catch (Exception e){
            // 没有记录

            int currentStep = entity.getCurrent_step();
            entity.setPrechare_step(entity.getCurrent_step());

            dao.insertStepInformation(entity);

            int coin = currentStep / 10;

            saveUserCoin(entity.getCustom_id(),coin);

            return  coin;
        }

    }



    private void saveUserCoin(String customId, int coin){

        try {
            coinService.saveUserCoin(customId,String.valueOf(coin));
        } catch (CommonException e) {
            e.printStackTrace();
        }

        // 保存金币记录
        coinRecordService.saveCoinRecord(customId,String.valueOf(coin),"步数兑换");
    }




    @Override
    public int findCanChangeCoin(StepEntity entity) throws CommonException {

        int currentStep = Integer.valueOf(entity.getCurrent_step());

        String customId = entity.getCustom_id();

        try {
            StepEntity stepEntity = dao.findRunStepWithCustomId(customId);

            int preStep = stepEntity.getPrechare_step();

            int num = currentStep - preStep;

            if (num > 0){
                return num / 10;
            }else {
                return 0;
            }

        }catch (Exception e){
            System.out.printf("没有记录");
            return entity.getCurrent_step() / 10;
        }

    }

    @Override
    public void clearUserStep() throws CommonException {
        dao.clearStepInformation();
    }





}
